package com.example.financeapi.controller;

import com.example.financeapi.entity.FMyPurchase;
import com.example.financeapi.enums.ResultEnum;
import com.example.financeapi.service.FMyPurchaseService;
import com.example.financeapi.entity.vo.ResultVOUtil;
import com.example.financeapi.entity.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的申购表(FMyPurchase)表控制层
 * @author Alvin
 */
@Slf4j
@RestController
@RequestMapping("fMyPurchase")
public class FMyPurchaseController {

    @Autowired
    private FMyPurchaseService fMyPurchaseService;

    /**
     * 我的打新记录列表
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getMyPurchaseList")
    public ResultVo getMyPurchaseList(HttpServletRequest request,
                                      @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                      @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        Map<String,Object> paramMap =new HashMap<>();
        String unionid = request.getServletContext().getAttribute("unionid")+"";
        try {
            if(StringUtils.isBlank(unionid)){
                return ResultVOUtil.error("unionid "+ResultEnum.PARAM_ERROR.getMessage()+",请重新登陆！");
            }
            paramMap.put("unionid",unionid);
            PageHelper.startPage(pageNum, pageSize);
            List<FMyPurchase> getMyPurchaseList =fMyPurchaseService.getMyPurchaseList(paramMap);
            PageInfo pageInfo =new PageInfo<>(getMyPurchaseList);
            return ResultVOUtil.success(pageInfo);
        } catch (Exception e) {
            log.error("【接口：getFiveKMMapList】后台异常，e={}" + e.getMessage(), e);
            return ResultVOUtil.error(ResultEnum.PROGRAM_EXCEPTION.getMessage() + e.getMessage(), e);
        }
    }

    /**
     * 申购或中签记录列表
     * @param flag 0申购 1中签
     * @param code 债劵代码
     * @param request
     * @return
     */
    @GetMapping("getPurchOrWinList")
    public ResultVo getPurchOrWinList(@RequestParam(defaultValue = "0", required = false)String flag,@RequestParam String code,HttpServletRequest request) {
        Map<String,Object> paramMap =new HashMap<>();
        String unionid = request.getServletContext().getAttribute("unionid")+"";
        try {
            if(!("0".equals(flag) || "1".equals(flag))){
                return ResultVOUtil.error(ResultEnum.PARAM_ERROR.getMessage());
            }
            if(StringUtils.isBlank(code)){
                return ResultVOUtil.error("债劵代码code"+ResultEnum.PARAM_IS_NULL.getMessage());
            }
            if(StringUtils.isBlank(unionid)){
                return ResultVOUtil.error("unionid "+ResultEnum.PARAM_ERROR.getMessage()+",请重新登陆！");
            }
            paramMap.put("code",code);
            paramMap.put("unionid",unionid);
            paramMap.put("flag",flag);
            List<FMyPurchase> getMyPurchaseList =fMyPurchaseService.getPurchOrWinList(paramMap);
            return ResultVOUtil.success(getMyPurchaseList);
        } catch (Exception e) {
            log.error("【接口：getFiveKMMapList】后台异常，e={}" + e.getMessage(), e);
            return ResultVOUtil.error(ResultEnum.PROGRAM_EXCEPTION.getMessage() + e.getMessage(), e);
        }
    }
}